
/**
 * RollupDataGrid is a menu button that supports fetching data in daily total,
 * montly total, and monthly average views. It takes the following properties:
 * 
 * dailyTotalUrl : (Required) the service url to be used to fetch daily total
 * data.
 * 
 * monthlyTotalUrl : (Required) the service url to be used to fetch monthly
 * total data.
 * 
 * monthlyAverageUrl : (Required) the service url to be used to fetch monthly
 * average data.
 * 
 * grid : (optional) - a reference to the grid, only necessary when the grid is
 * not part of the ancestory of this component.
 * 
 */
Ext.define('AmpleHarvest.objects.Statistics.button.cRollupDataGrid',
{
	extend						: 'Ext.button.Button',
	alias						: 'widget.metricsviewer-view-buttons-rollupdatagrid',

	text						: 'View: Daily Totals',
	showText					: true,

	getGrid						: function()
	{
		if (!this.grid)
		{
			this.grid = this.up('gridpanel');
		}
		return this.grid;
	},

	getDailyTotalUrl			: function()
	{
		return this.dailyTotalUrl;
	},

	getMonthlyTotalUrl			: function()
	{
		return this.monthlyTotalUrl;
	},

	getMonthlyAverageUrl		: function()
	{
		return this.monthlyAverageUrl;
	},

	isDailyViewActive			: function()
	{
		if (this.text.indexOf("Daily") !== -1)
		{
			return true;
		}
		return false;
	},

	setDailyViewActive			: function()
	{
		this.setText(this.getViewDailyTotals());
	},

	menuSelectionHandler		: function(newViewText, url, param)
	{
		var store, proxy;
		store = this.getGrid().getStore();
		proxy = store.getProxy();
		if (this.text !== newViewText && url)
		{
			this.setText(newViewText);
			proxy.url = url;
			proxy.extraParams.view = param;
			store.loadPage(1);
		}
	},

	setViewToDailyTotals		: function()
	{
		this.text = this.getViewDailyTotals();
	},

	setViewToMonthlyTotals		: function()
	{
		this.text = this.getViewMonthlyTotals();
	},

	setViewToMonthlyAverages	: function()
	{
		this.text = this.getMonthlyAverages();
	},

	getViewDailyTotals			: function()
	{
		return 'View: Daily Totals';
	},

	getViewMonthlyTotals		: function()
	{
		return 'View: Monthly Totals';
	},

	getViewMonthlyAverages		: function()
	{
		return 'View: Monthly Averages';
	},

	constructor					: function()
	{
		this.menu =
		{
			items	:
			[
					{
						text		: 'Daily Totals',
						listeners	:
						{
							click	:
							{
								fn		: function()
								{
									var viewText = this.getViewDailyTotals();
									if (this.text !== viewText)
									{
										this
												.menuSelectionHandler(
														viewText,
														this.getDailyTotalUrl(),
														TOTDAY
												);
									}
								},
								scope	: this
							}
						}

					},
					{
						text		: 'Monthly Totals',
						listeners	:
						{
							click	:
							{
								fn		: function()
								{
									var viewText = this.getViewMonthlyTotals();
									if (this.text !== viewText)
									{
										this
												.menuSelectionHandler(
														viewText,
														this.getMonthlyTotalUrl(),
														TOTMON
												);
									}
								},
								scope	: this
							}
						}
					},
					{
						text		: 'Monthly Averages',
						listeners	:
						{
							click	:
							{
								fn		: function()
								{
									var viewText = this
											.getViewMonthlyAverages();
									if (this.text !== viewText)
									{
										this
												.menuSelectionHandler(
														viewText,
														this.getMonthlyAverageUrl(),
														AVGDAY
												);
									}
								},
								scope	: this
							}
						}
					}
			]
		};

		this.callParent(arguments);

	}

}
);